/**
 * scence方法
 * */


var videoList = []; //视频数组


//获取视频场景列表
function getVideoList(){
    //clearCache();
    $.ajax({
        type: apis.scenceVideoList.method,
        url: domain + apis.scenceVideoList.url,
        data: {uid: getUid()},
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                console.log(res.msg);
            }
            videoList = res.data;
            $('#boxList').html(videoListHtml());
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//视频拼接字符串
function videoListHtml(){
    var h = '', vid, vlink;
    for (var i = 0; i < videoList.length; i ++) {
        vid = videoList[i].id;
        vlink = videoList[i].link;
        h += '<div class="boxOne" style="width: ' + (cw * 0.5 - 2) + 'px;">';
        h += '<img src="' + domain + videoList[i].cover + '"/>';
        h += '<div class="title">'+videoList[i].title+'</div>';
        h += '<div class="menu" style="left: ' + (cw * 0.5 - 40) + 'px;">';
        h += '<div><img src="../../imgs/mpPlay.png"/></div>';
        h += '<div>' + videoList[i].playNum + '</div>';
        h += '<div style="height: 5px;"></div>';
        h += '<div><img src="../../imgs/mpUse.png"/></div>';
        h += '<div>' + videoList[i].useNum + '</div>';
        h += '</div>';
        h += '<div class="playbtn" onclick="getPlayPop(' + vid + ');">';
        h += '<div class="playbg"></div>';
        h += '<img src="../../imgs/mpPlay.png"/>';
        h += '<div class="playtxt">点击播放</div>';
        h += '<input type="hidden" id="vlink'+vid+'" value="' + vlink + '"/>';
        h += '<input type="hidden" id="scenceId'+vid+'" value="' + videoList[i].scenceId + '"/>';
        h += '<input type="hidden" id="userType'+vid+'" value="' + videoList[i].userType + '"/>';
        h += '<input type="hidden" id="viewName'+vid+'" value="' + videoList[i].viewName + '"/>';
        h += '<input type="hidden" id="playNum'+vid+'" value="' + videoList[i].playNum + '"/>';
        h += '<input type="hidden" id="useNum'+vid+'" value="' + videoList[i].useNum + '"/>';
        h += '</div>';
        h += '</div>';
    }
    return h;
}


//播放弹窗
var isPlayEnd = 0; //是否播放结束：0没有结束，1已结束
function getPlayPop(vid){
    var cwClose = cw - 50, chbody = ch - 35;
    var vlink = $('#vlink' + vid).val();
    var scenceId = $('#scenceId' + vid).val();
    var playNum = $('#playNum' + vid).val();
    var useNum = $('#useNum' + vid).val();
    if (vlink == '') {
        msgPop({msg:'没有视频'}); return
    }
    var h = '';
    h += '<div style="width: ' + cw + 'px; height: ' + chbody + 'px; background: #000; position: fixed; top: 35px; z-index: 100;">';
    //h += '<video src="http://api.jiuge_freecode.com/videos/2024/20240202.mp4" style="width: '+cw+'px; height: '+chbody+'px;" autoplay loop></video>';
    h += '<video src="' + domain + vlink + '" style="width: ' + cw + 'px; height: ' + chbody + 'px;" autoplay loop></video>';
    h += '<div id="menuPop" style="left: ' + (cw - 50) + 'px;">';
    h += '<div><img src="../../imgs/mpPlay.png"/></div>';
    h += '<div>' + playNum + '</div>';
    h += '<div style="height: 5px;"></div>';
    h += '<div onclick="getForm(' + vid + ');"><img src="../../imgs/mpUse.png"/></div>';
    h += '<div onclick="getForm(' + vid + ');">' + useNum + '</div>';
    h += '</div>';
    h += '<div style="color: red; font-weight: bolder; position: relative; left: ' + cwClose + 'px; top: -' + (chbody - 10) + 'px; z-index: 100;" onclick="$(\'#playPop\').html(\'\');">关闭</div>';
    h += '</div>';
    $('#playPop').html(h);
    addPlay(vid); //新增播放
    if (isPlayEnd == 0) {
        setInterval(function () {
            addDelay({vid: vid, delay: 5});
        }, 5000); //新增时长
    }
}


//新增播放
function addPlay(vid){
    var userInfo = getUserInfo();
    var formData = {
        uid: getUid(),
        videoId: vid
    };
    console.log(formData);
    if (!userInfo || !userInfo.userId){
        window.location.href = apis.index + '?code=' + code;
        return
    }
    if (!vid){
        msgPop({msg: '没有视频'});
        return
    }
    $.ajax({
        type: apis.scenceVideoPlay.method,
        url: domain + apis.scenceVideoPlay.url,
        data: formData,
        //headers: {
        //    'Content-Type': 'application/json',
        //    code: code,
        //    userId: userInfo.userId
        //},
        success: function (res) {
            console.log(res);
        },
        error: function (res) {
            console.log('网络错误');
        }
    });
}


//新增时长
function addDelay(e){
    var userInfo = getUserInfo();
    var formData = {
        uid: getUid(),
        videoId: e.vid, delay: e.delay
    };
    if (!userInfo || !userInfo.userId){
        window.location.href = routes.index + '?code=' + code;
        return
    }
    if (!e.vid){
        msgPop({msg: '没有视频'});
        return
    }
    $.ajax({
        type: apis.scenceVideoDelay.method,
        url: domain + apis.scenceVideoDelay.url,
        data: formData,
        success: function (res) {
            console.log(res);
            //if (res.code != 0) {
            //    msgPop({msg: res.msg});
            //    return
            //}
            if (res.code == 2) {
                isPlayEnd = 1;
            }
        },
        error: function (res) {
            console.log('网络错误');
        }
    });
}


//获取表单
function getForm(vid){
    var viewName = $('#viewName' + vid).val();
    var userType = $('#userType' + vid).val();
    //console.log(viewName, userType); return;
    if (!viewName) {
        msgPop({msg: '没有页面参数'});
        return
    }
    if (userType == 2) { //商户功能只能APP编辑
        appPop();
        return
    }
    var toUrl = '/views/' + viewName + '/add.html?code=' + code + '&vid=' + vid;
    console.log(toUrl);
    window.location.href = toUrl;
}